
<link rel="stylesheet" type="text/css" href="/static/index/css/pages-cart.css" />
<!--主内容-->
<div class="cart py-container">
	<!--All goods-->
	<div class="allgoods">
		<h4>全部商品</h4>
		<div class="cart-main">
			<div class="yui3-g cart-th">
				<div class="yui3-u-1-4"><input type="checkbox" class="check_all" name="" id="" value="" /> 全部</div>
				<div class="yui3-u-1-4">商品</div>
				<div class="yui3-u-1-8">单价（元）</div>
				<div class="yui3-u-1-8">数量</div>
				<div class="yui3-u-1-8">小计（元）</div>
				<div class="yui3-u-1-8">操作</div>
			</div>
			<div class="cart-item-list">
				<div class="cart-body">
					{foreach $data as $v}
						<div class="cart-list">
							<ul class="goods-list yui3-g" cart_id="{$v.id}" number="{$v.cart_number}" goods_id="{$v.goods_id}" goods_attr_ids="{$v.goods_attr_ids}">
								<li class="yui3-u-1-24">
									<input type="checkbox" class="row_check" name="" id="" value="" />
								</li>
								<li class="yui3-u-6-24">
									<div class="good-item">
										<div class="item-img" style="margin-right: 15px;">
											<img src="{$v.goods.goods_logo}" width="120" height="80"/>
										</div>
										{foreach $v.goods_attr as $m}
											<div class="item-msg" >
												{$m.attr_name} : {$m.attr_value}
											</div>
										{/foreach}
									</div>
								</li>
								<li class="yui3-u-5-24">
										<div class="item-txt">
											{$v.goods.goods_name}
										</div>
								</li>
								<li class="yui3-u-1-8"><span class="price">{$v.goods.goods_price}</span></li>
								<li class="yui3-u-1-8">
									<a href="javascript:void(0)" class="increment mins">-</a>
									<input autocomplete="off" type="text" attr value="{$v.cart_number}" minnum="1" class="itxt current_number" />
									<a href="javascript:void(0)" class="increment plus">+</a>
								</li>
								<li class="yui3-u-1-8"><span class="sum">{$v.goods.goods_price * $v.cart_number}</span></li>
								<li class="yui3-u-1-8">
									<a href="#none" class="delete">删除</a><br />
<!--									<a href="#none">移到我的关注</a>-->
								</li>
							</ul>
						</div>
					{/foreach}
				</div>
			</div>
		</div>
		<div class="cart-tool">
			<div class="select-all">
				<input type="checkbox" class="check_all" name="" value="" />
				<span>全选</span>
			</div>
			<div class="option">
				<a href="#none">删除选中的商品</a>
<!--				<a href="#none">移到我的关注</a>-->
			</div>
			<div class="money-box">
				<div class="chosed">已选择<span id="total_number">0</span>件商品</div>
				<div class="sumprice">
					<span><em>总价（不含运费） ：</em><i id="total_price" class="summoney">¥0</i></span>
					<span><em>已节省：</em><i>-¥0</i></span>
				</div>
				<div class="sumbtn">
					<a class="sum-btn" href="javascript:;">结算</a>
				</div>
			</div>
		</div>
		<div class="clearfix"></div>
		<!--<div class="deled">
			<span>已删除商品，您可以重新购买或加关注：</span>
			<div class="cart-list del">
				<ul class="goods-list yui3-g">
					<li class="yui3-u-1-2">
						<div class="good-item">
							<div class="item-msg">Apple Macbook Air 13.3英寸笔记本电脑 银色（Corei5）处理器/8GB内存</div>
						</div>
					</li>
					<li class="yui3-u-1-6"><span class="price">8848.00</span></li>
					<li class="yui3-u-1-6">
						<span class="number">1</span>
					</li>
					<li class="yui3-u-1-8">
						<a href="#none">重新购买</a>
						<a href="#none">移到我的关注</a>
					</li>
				</ul>
			</div>
		</div>-->
		<div class="liked">
			<ul class="sui-nav nav-tabs">
				<li class="active">
					<a href="#index" data-toggle="tab">猜你喜欢</a>
				</li>
				<li>
					<a href="#profile" data-toggle="tab">特惠换购</a>
				</li>
			</ul>
			<div class="clearfix"></div>
			<div class="tab-content">
				<div id="index" class="tab-pane active">
					<div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
						<div class="carousel-inner">
							<div class="active item">
								<ul>
									<li>
										<img src="/static/index/img/like1.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
									<li>
										<img src="/static/index/img/like2.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
									<li>
										<img src="/static/index/img/like3.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
									<li>
										<img src="/static/index/img/like4.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
								</ul>
							</div>
							<div class="item">
								<ul>
									<li>
										<img src="/static/index/img/like1.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
									<li>
										<img src="/static/index/img/like2.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
									<li>
										<img src="/static/index/img/like3.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
									<li>
										<img src="/static/index/img/like4.png" />
										<div class="intro">
											<i>Apple苹果iPhone 6s (A1699)</i>
										</div>
										<div class="money">
											<span>$29.00</span>
										</div>
										<div class="incar">
											<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i class="car"></i><span class="cartxt">加入购物车</span></a>
										</div>
									</li>
								</ul>
							</div>
						</div>
						<a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
						<a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
					</div>
				</div>
				<div id="profile" class="tab-pane">
					<p>特惠选购</p>
				</div>
			</div>
		</div>
	</div>
</div>

<script>

	$(function(){

		// 给加入购物车绑定事件
		$(".sum-btn").click(function(){

			var cart_ids = "";

			$(".row_check:checked").each(function(i,v){

				cart_ids += $(v).closest("ul").attr("cart_id")+',';
			});

			if(cart_ids == ""){
				alert("亲，请添加要购买的商品");
				return;
			}
			cart_ids = cart_ids.slice(0,-1);

			location.href = "{:url('index/order/add')}" + "?cart_ids=" + cart_ids;
		});

		// 删除购物车数据
		$(".delete").click(function(){
			if(!confirm("您确定要删除吗")){
				return
			};
			var date = {
				"goods_id" : parseInt($(this).closest("ul").attr("goods_id")),
				'goods_attr_ids' : $(this).closest("ul").attr('goods_attr_ids')
			};

			var that = this;
			$.ajax({
				url:"{:url('index/cart/delete')}",
				data:date,
				success:function(res){

					if(res.code != 10000){

						alert(res.msg);
						return;
					}
					$(that).closest("ul").parent().remove();
					changeTotal();
				}
			})
		});

		// 封装ajax函数
		var changenum = function(element,number){
			var date = {
				'goods_id':$(element).closest("ul").attr("goods_id"),
				'goods_attr_ids':$(element).closest("ul").attr("goods_attr_ids"),
				'cart_number':number
			};

			$.ajax({
				url:"{:url('index/cart/changenum')}",
				data:date,
				success:function(res){
					if(res.code != 10000)
					{
						alert(res.msg);
						return;
					}

				}
			})

		};

		// 封装函数 计算总价和总数量，
		var changeTotal = function(){

			var total_number = 0;
			var total_price = 0;

			$(".row_check:checked").each(function(i,v){

				total_number += parseInt($(v).closest('ul').find('.current_number').val());

				total_price += parseFloat($(v).closest('ul').find(".sum").html());
			})

			$("#total_number").html(total_number);
			$("#total_price").html("￥"+total_price);
		};

		// 全选的选中状态
		$(".check_all").change(function(){

			var status = $(this).prop('checked');

			$(".row_check").prop("checked",status);
			$(".check_all").prop("checked",status);
			changeTotal();
		});

		// 单选的选中状态
		$(".row_check").change(function(){

			var total = $(".row_check").length;

			// 选中行数
			var checked = $('row_check:checked').length;

			var status = total == checked;

			$(".check_all").prop("checked",status);

			changeTotal();

		});

		// 给input绑定onchange事件
		$(".current_number").change(function(){

			var number = $(this).val();
			var old_number = parseInt($(this).closest("ul").attr("number"));

			if(isNaN(number)){

				alert("亲不许是数字哦")
				$(this).val(old_number);

				return;
			}

			if(parseInt(number) != number){

				alert("数量不能是小数");
				$(this).val(old_number);
				return;
			}

			if(parseInt(number) > 1 || parseInt(number) < 100){

				alert("数量不能超过100也不可以小于1");
				$(this).val(old_number);
				return;
			}

		});

		// 所有的加号点击事件
		$(".plus").click(function(){

			var number = parseInt($(this).closest('ul').find('.current_number').val());

			if(isNaN(number)){

				alert("必须是整数哦");
				$(this).closest('ul').find('.current_number').val(1);
				return;
			}

			number+=1;

			$(this).closest("ul").find('.current_number').val(number);

			// 计算小计金额
			var price = parseFloat($(this).closest('ul').find('.price').html());

			var sum = parseFloat(number * price);

			$(this).closest("ul").find('.sum').html(sum);

			changeTotal();

			// ajax点击事件
			changenum(this,number);
		});

		// 给所有的减号添加点击事件
		$(".mins").click(function(){

			var number = parseInt($(this).closest("ul").find('.current_number').val());

			if(isNaN(number)){
				alert("必须是整数哦");
				$(this).closest('ul').find('.current_number').val(1);
				return;
			}

			if(number == 1){
				alert("亲不能在减了");
				$(this).closest('ul').find('.current_number').val(1);
				return;
			}
			number -= 1;

			$(this).closest("ul").find('.current_number').val(number);

			var price = parseFloat($(this).closest("ul").find('.price').html());
			var sum = parseFloat(price * number);

			$(this).closest("ul").find('.sum').html(sum);

			changeTotal();
			// ajax点击事件
			changenum(this,number);
		});
	})

</script>